home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u118.dms / in.adf / iff / showilbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-30  |  9.6 KB  |  303 lines

  1. /** ShowILBM.c **************************************************************
  2.  *
  3.  * Read an ILBM raster image file and display it.      24-Jan-86.
  4.  *
  5.  * By Jerry Morrison, Steve Shaw, and Steve Hayes, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * USE THIS AS AN EXAMPLE PROGRAM FOR AN IFF READER.
  9.  *
  10.  * The IFF reader portion is essentially a recursive-descent parser.
  11.  * The display portion is specific to the Commodore Amiga computer.
  12.  *
  13.  * NOTE: This program displays an image, pauses, then exits.
  14.  *
  15.  * Usage from CLI:
  16.  *   showilbm picture1 [picture2] ...
  17.  *
  18.  * Usage from WorkBench:
  19.  * Click on ShowILBM, hold down shift key, click on each picture to show,
  20.  * Double-click on final picture to complete the selection, release the
  21.  * shift key.
  22.  *
  23.  ****************************************************************************/
  24.  
  25. /* If you are constructing a Makefile, here are the names of the files 
  26.  * that you'll need to compile and link with to use showilbm:
  27.  
  28.     showilbm.c
  29.     readpict.c
  30.     remalloc.c
  31.     ilbmr.c
  32.     iffr.c
  33.     unpacker.c
  34.     gio.c
  35.  
  36.     and you'll have to get movmem() from lc.lib
  37.  
  38.  * robp.
  39.  * ********************************************************************** */
  40.  
  41.  
  42. #include "iff/intuall.h"
  43. #include "libraries/dos.h"
  44. #include "libraries/dosextens.h"
  45. #include "iff/ilbm.h"
  46. #include "workbench/workbench.h"
  47. #include "workbench/startup.h"
  48. #include "iff/readpict.h"
  49. #include "iff/remalloc.h"
  50.  
  51. #define LOCAL static
  52.  
  53. #define MIN(a,b) ((a)<(b)?(a):(b))
  54. #define MAX(a,b) ((a)>(b)?(a):(b))
  55.  
  56. /* general usage pointers */
  57. struct GfxBase *GfxBase;
  58. LONG IconBase;    /* Actually, "struct IconBase *" if you've got some ".h" file*/
  59.  
  60. /* For displaying an image */
  61. LOCAL struct RastPort rP;
  62. LOCAL struct BitMap bitmap0;
  63. LOCAL struct RasInfo rasinfo;
  64. LOCAL struct View v = {0};
  65. LOCAL struct ViewPort vp = {0};
  66.  
  67. LOCAL ILBMFrame iFrame;
  68.     
  69. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  70. #define bufSz 512
  71.  
  72. #ifdef NORTHC
  73. /* NorthC cannot cope with strings in brackets, it assumes that you will be 
  74.    dealing with the individual characters if you use brackets */
  75.  
  76. /* Message strings for IFFP codes. */
  77. LOCAL char MsgOkay[]        = 
  78.     "(IFF_OKAY) Didn't find a FORM ILBM in the file.";
  79. LOCAL char MsgEndMark[]     = "(END_MARK) How did you get this message?";
  80. LOCAL char MsgDone[]        = "(IFF_DONE) All done.";
  81. LOCAL char MsgDos[]         = "(DOS_ERROR) The DOS returned an error.";
  82. LOCAL char MsgNot[]         = "(NOT_IFF) Not an IFF file.";
  83. LOCAL char MsgNoFile[]      = "(NO_FILE) No such file found.";
  84. LOCAL char MsgClientError[] = 
  85.     "(CLIENT_ERROR) ShowILBM bug or insufficient RAM.";
  86. LOCAL char MsgForm[]        = "(BAD_FORM) A malformed FORM ILBM.";
  87. LOCAL char MsgShort[]       = "(SHORT_CHUNK) A malformed FORM ILBM.";
  88. LOCAL char MsgBad[]         = "(BAD_IFF) A mangled IFF file.";
  89. #else
  90. /* Message strings for IFFP codes. */
  91. LOCAL char MsgOkay[]        = {
  92.     "(IFF_OKAY) Didn't find a FORM ILBM in the file." };
  93. LOCAL char MsgEndMark[]     = { "(END_MARK) How did you get this message?" };
  94. LOCAL char MsgDone[]        = { "(IFF_DONE) All done."};
  95. LOCAL char MsgDos[]         = { "(DOS_ERROR) The DOS returned an error." };
  96. LOCAL char MsgNot[]         = { "(NOT_IFF) Not an IFF file." };
  97. LOCAL char MsgNoFile[]      = { "(NO_FILE) No such file found." };
  98. LOCAL char MsgClientError[] = {
  99.     "(CLIENT_ERROR) ShowILBM bug or insufficient RAM."};
  100. LOCAL char MsgForm[]        = { "(BAD_FORM) A malformed FORM ILBM." };
  101. LOCAL char MsgShort[]       = { "(SHORT_CHUNK) A malformed FORM ILBM." };
  102. LOCAL char MsgBad[]         = { "(BAD_IFF) A mangled IFF file." };
  103. #endif
  104.  
  105. /* THESE MUST APPEAR IN RIGHT ORDER!! */
  106. #ifdef NORTHC
  107. /* NorthC treats casts as complex operations, even simple ones.  So we must
  108.    remove the cast to allow NorthC to use the size here as a constant */
  109. LOCAL char *IFFPMessages[-LAST_ERROR+1] = {
  110. #else
  111. LOCAL char *IFFPMessages[-(int)LAST_ERROR+1] = {
  112. #endif
  113.     /*IFF_OKAY*/  MsgOkay,
  114.     /*END_MARK*/  MsgEndMark,
  115.     /*IFF_DONE*/  MsgDone,
  116.     /*DOS_ERROR*/ MsgDos,
  117.     /*NOT_IFF*/   MsgNot,
  118.     /*NO_FILE*/   MsgNoFile,
  119.     /*CLIENT_ERROR*/ MsgClientError,
  120.     /*BAD_FORM*/  MsgForm,
  121.     /*SHORT_CHUNK*/  MsgShort,
  122.     /*BAD_IFF*/   MsgBad
  123.     };
  124.  
  125. /** DisplayPic() ************************************************************
  126.  *
  127.  * Interface to Amiga graphics ROM routines.
  128.  *
  129.  ****************************************************************************/
  130. DisplayPic(bm, ptilbmFrame)
  131.     struct BitMap *bm;  ILBMFrame *ptilbmFrame;  {
  132.     int i;
  133.     struct View *oldView = GfxBase->ActiView;    /* so we can restore it */
  134.  
  135.     InitView(&v);
  136.     InitVPort(&vp);
  137.     v.ViewPort = &vp;
  138.     InitRastPort(&rP);
  139.     rP.BitMap = bm;
  140.     rasinfo.BitMap = bm;
  141.  
  142.     /* Always show the upper left-hand corner of this picture. */
  143.     rasinfo.RxOffset = 0;
  144.     rasinfo.RyOffset = 0;
  145.  
  146.     vp.DWidth = MAX(ptilbmFrame->bmHdr.w, 4*8);
  147.     vp.DHeight = ptilbmFrame->bmHdr.h;
  148.  
  149. #if 0
  150.     /* Specify where on screen to put the ViewPort. */
  151.     vp.DxOffset = ptilbmFrame->bmHdr.x;
  152.     vp.DyOffset = ptilbmFrame->bmHdr.y;
  153. #else
  154.     /* Always display it in upper left corner of screen.*/
  155. #endif
  156.  
  157.     if (ptilbmFrame->bmHdr.pageWidth <= 320) 
  158.     vp.Modes = 0;
  159.     else vp.Modes = HIRES;
  160. #ifndef NORTHC
  161.     /* For some as yet unknown reason all compressed pictures seem to come 
  162.        out at 256 high, even thos that are not, this is a pure hack */
  163.     if (ptilbmFrame->bmHdr.pageHeight > 200) {
  164.     v.Modes |= LACE;
  165.     vp.Modes |= LACE;
  166.     }
  167. #endif
  168.     vp.RasInfo = &rasinfo;
  169.     MakeVPort(&v,&vp);
  170.     MrgCop(&v);
  171.     LoadView(&v);    /* show the picture */
  172.     WaitBlit();
  173.     WaitTOF();
  174.     LoadRGB4(&vp, ptilbmFrame->colorMap, ptilbmFrame->nColorRegs);
  175.  
  176.     for (i = 0; i < 5*60; ++i)  WaitTOF();    /* Delay 5 seconds. */
  177.  
  178.     LoadView(oldView);    /* switch back to old view */
  179.     }
  180.  
  181. /** stuff for main0() *******************************************************/
  182. LOCAL struct WBStartup *wbStartup = 0;    /* 0 unless started from WorkBench.*/
  183.  
  184. PrintS(msg)  char *msg; {
  185.     if (!wbStartup)  printf(msg);
  186.     }
  187.  
  188. void GoodBye(msg)  char *msg; {
  189. /*     PrintS(msg);   PrintS("\n");  */
  190.     printf(msg);   printf("\n");    /* If linked with Lstartup.obj and
  191.                      * NOT compiled with -dTINY, this 
  192.                      * outputs the message to the window
  193.                      * that Lattice opens.
  194.                      * ... carolyn.
  195.                      */
  196.     exit(0);
  197.     }
  198.  
  199. /** OpenArg() ***************************************************************
  200.  *  Given a "workbench argument" (a file reference) and an I/O mode.
  201.  *  It opens the file.
  202.  ****************************************************************************/
  203. LONG OpenArg(wa, openmode)  struct WBArg *wa;   int openmode; {
  204.     LONG olddir;
  205.     LONG file;
  206.     if (wa->wa_Lock)   olddir = CurrentDir(wa->wa_Lock);
  207.     file = Open(wa->wa_Name, openmode);
  208.     if (wa->wa_Lock)   CurrentDir(olddir);
  209.     return(file);
  210.     }
  211.  
  212. /** main0() *****************************************************************/
  213. void main0(wa)  struct WBArg *wa;  {
  214.     LONG file;
  215.     IFFP iffp = NO_FILE;
  216.  
  217.     /* load and display the picture */
  218.     file = OpenArg(wa, MODE_OLDFILE);
  219.     if (file)
  220.     iffp = ReadPicture(file, &bitmap0, &iFrame, ChipAlloc);
  221.     /* Allocates BitMap using ChipAlloc().*/
  222.     Close(file);
  223.     if (iffp == IFF_DONE)
  224.     DisplayPic(&bitmap0, &iFrame);
  225.  
  226. /*     PrintS(" ");   PrintS(IFFPMessages[-iffp]);   PrintS("\n");   */
  227.      printf(" ");   printf(IFFPMessages[-iffp]);   printf("\n");  
  228.     /* see note near definition of PrintS */
  229.  
  230.     /* cleanup */
  231.     if (bitmap0.Planes[0])  {
  232.     RemFree(bitmap0.Planes[0]);
  233.         /* ASSUMES allocated all planes via a single ChipAlloc call.*/
  234.     FreeVPortCopLists(&vp);
  235.     FreeCprList(v.LOFCprList);
  236.     }
  237.     }
  238.  
  239. extern struct WBStartup *WBenchMsg;    /* added: Carolyn Scheppner */
  240.  
  241. /** main() ******************************************************************/
  242.  
  243. void main(argc, argv)  int argc;  char **argv;  {
  244.     struct WBArg wbArg, *wbArgs;
  245.     LONG olddir;
  246. /*sss    struct Process *myProcess; */
  247.  
  248.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) )
  249.     GoodBye("No graphics.library");
  250.     if( !(IconBase = OpenLibrary("icon.library",0)) )
  251.     GoodBye("No icon.library");
  252.     if (!argc) {
  253.     /* Invoked via workbench */
  254. /*     wbStartup = (struct WBStartup *)argv;   */
  255. #ifndef NORTHC
  256.         /* NorthC passes the workbench message on in a different way, in 
  257.            fact with NorthC you do not need to do any of this messing 
  258.            with the message as the arguments are alredy extracted from 
  259.            it for you */
  260.     wbStartup = WBenchMsg;    /* modified by Carolyn Scheppner */
  261. #endif
  262.     wbArgs = wbStartup->sm_ArgList;
  263.     argc = wbStartup->sm_NumArgs;
  264.     while (argc >= 2) {
  265.         olddir = CurrentDir(wbArgs[1].wa_Lock);
  266.         main0(&wbArgs[1]);
  267.         argc--;   wbArgs = &wbArgs[1];
  268.         }
  269. #if 0
  270.     /* [TBD] We want to get an error msg to the Workbench user... */
  271.     if (argc < 2) {
  272.         printf ("Usage from workbench:\n");
  273.         printf (" Click mouse on Show-ILBM, Then hold 'SHIFT' key\n");
  274.  
  275.     /* BOTH OF THESE WERE "PrintS", see note near PrintS definition */
  276.  
  277.         GoodBye(" while double-click on file to display.");
  278.         }
  279. #endif
  280.     }
  281.     else {
  282.     /* Invoked via CLI.  Make a lock for current directory.
  283.      * Eventually, scan name, separate out directory reference?*/
  284.     if (argc < 2)
  285.         GoodBye("Usage from CLI: 'Show-ILBM filename'");
  286. /*sss    myProcess = (struct Process *)FindTask(0); */
  287.     wbArg.wa_Lock = 0; /*sss myProcess->pr_CurrentDir; */
  288.     while (argc >= 2) {
  289.         wbArg.wa_Name = argv[1];
  290.         printf("Showing file ");   printf(wbArg.wa_Name);   printf(" ...");
  291.     /* THESE WERE "PrintS", see note near PrintS definition */
  292.         main0(&wbArg);
  293.         printf("\n");
  294.     /* THIS WAS "PrintS", see note near PrintS definition */
  295.         argc--;   argv = &argv[1];
  296.         }
  297.     }
  298.     CloseLibrary(GfxBase);
  299.     CloseLibrary(IconBase);
  300.     exit(0);
  301.     }
  302.  
  303.